﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WeighClient.Utils
{
    public class CardUtil
    {
        public static String getCardGUID()
        {
            String result = "";

            String uuid = Guid.NewGuid().ToString();

            uuid = uuid.Replace("-", "").ToUpper();

            Console.WriteLine(uuid.Length);
            return uuid;
        }

        public static String getCardGUIDCode(String code)
        {
            String result = "FFCC017A383130363138";

            for (int i = 0; i < code.Length; i++)
            {
                String charStr = code.Substring(i, 1);
                byte[] array = System.Text.Encoding.ASCII.GetBytes(charStr);
                String hexAscii = ByteUtils.byteToHexStr(array);
                //Console.WriteLine(charStr + "  " + hexAscii);
                result += hexAscii;
            }
            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static String getCarNumCode(String carNum, String prodCode, String num)
        {
            //车号、油品代码、预装量
            String result = "FF CC 01 7B 38  31 30 36 31 38";
            result = result.Replace(" ", "");

            result += "313437";

            String carNum2 = carNum.Substring(1, 6);
            String carNumHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(carNum2));
            result += carNumHexAscii;

            if (prodCode.Length == 1)
            {
                prodCode = "0" + prodCode;
            }
            String prodCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(prodCode));
            result += prodCodeHexAscii;

            try
            {
                num = num.PadRight(5, '0');
            }
            catch (ArgumentOutOfRangeException ex)
            {
                num = "00000";
            }
            String numCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(num));
            result += numCodeHexAscii;

            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static String getIcCode(String code)
        {
            String result = "FF CC 01 71 38  31 30 36 31 38";
            result = result.Replace(" ", "");

            String icCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(code));
            result += icCodeHexAscii;

            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static String getBillCode(String code)
        {
            String result = "FF CC 01 79 38  31 30 36 31 38";
            result = result.Replace(" ", "");

            String billCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(code));
            result += billCodeHexAscii;

            result = result + ByteUtils.CalcXor(result);

            return result;
        }
    }
}
